#!/bin/sh
APIkey='48162FF9-8994-460B-87E1-83F697954F89'
IP='10.64.71.8'
ClientUUID='BD7D3490-47A0-A060-EB48-B8144E09F0C2'
upload_tgz_file='./a.tar.gz'





upload_start=0
checksum_str_file='./tmp'

curlCmd()
{
 checksum_str=$APIkey

 for i in $*;do

     [ "${i:0:8}" = "https://" ] && echo "Connecting $i ..." 	 

     if [ "${i::7}" = "X-DTAS-" ];then
	     checksum_str=$checksum_str$(echo $i|awk -F':' '{print $2}')
	 fi

 done
	 echo -n $checksum_str >$checksum_str_file

	 if [ $upload_start -eq 1 ];then
   	      echo "Add body checksum"
          tmp_f=`dd if=$upload_tgz_file bs=$(ls -l $upload_tgz_file|awk '{print $5}') count=1`
		  cat $upload_tgz_file >>$checksum_str_file
     fi

  checksum=$(sha1sum $checksum_str_file|awk '{print $1}')
  curl -k -H "X-DTAS-Checksum:$checksum" $* 
}
curlCmd $* -H 'X-DTAS-Challenge:921694F4-5A0C-C8EC-E3CD-5650F7480D36'\
	     -H "X-DTAS-ClientUUID:$ClientUUID"\
		 -H 'X-DTAS-ProtocolVersion:1.4'\
		 -H 'X-DTAS-SourceID:100'\
		 -H 'X-DTAS-SourceName:ddi'\
		 -H 'X-DTAS-Time:1464825757'\
		 https://$IP/web_service/sample_upload/test_connection
		 
curlCmd $* -H 'X-DTAS-Challenge:921694F4-5A0C-C8EC-E3CD-5650F7480D36'\
	     -H 'X-DTAS-ClientHostname:CentOS7'\
	     -H "X-DTAS-ClientUUID:$ClientUUID"\
		 -H 'X-DTAS-ProtocolVersion:1.4'\
		 -H 'X-DTAS-ProductName:DDI'\
		 -H 'X-DTAS-SourceID:100'\
		 -H 'X-DTAS-SourceName:ddi'\
		 -H 'X-DTAS-Time:1464825757'\
		 https://$IP/web_service/sample_upload/register



[ ! -e "$upload_tgz_file" ] && echo "No archive  to upload, exit" && exit
tarfile_sha1=$(sha1sum $upload_tgz_file|awk '{print $1}')
tarfile_name="20160602-174658_$tarfile_sha1.tgz" 
tarfile_size=$(ls -l   $upload_tgz_file |awk '{print $5}')

upload_start=1


curlCmd $* -X 'PUT' -H "X-DTAS-Archive-Filename:$tarfile_name"\
	     -H "X-DTAS-Archive-SHA1:$tarfile_sha1"\
 		 -H 'X-DTAS-Challenge:921694F4-5A0C-C8EC-E3CD-5650F7480D36'\
	     -H 'X-DTAS-ClientHostname:CentOS7'\
	     -H "X-DTAS-ClientUUID:$ClientUUID"\
		 -H 'X-DTAS-ProtocolVersion:1.4'\
		 -H 'X-DTAS-ProductName:DDI'\
		 -H 'X-DTAS-SourceID:100'\
		 -H 'X-DTAS-SourceName:ddi'\
		 -H 'X-DTAS-Time:1464825757'\
		 -H 'Content-Type:application/x-compressed'\
		 -H  "Content-Length:$tarfile_size"\
		 --data-binary @"$upload_tgz_file"\
		 https://$IP/web_service/sample_upload/upload_sample

upload_start=0


curlCmd $* -H 'X-DTAS-Challenge:921694F4-5A0C-C8EC-E3CD-5650F7480D36'\
	     -H 'X-DTAS-ClientHostname:CentOS7'\
	     -H "X-DTAS-ClientUUID:$ClientUUID"\
		 -H 'X-DTAS-ProtocolVersion:1.4'\
		 -H 'X-DTAS-ProductName:DDI'\
		 -H 'X-DTAS-SourceID:100'\
		 -H 'X-DTAS-SourceName:ddi'\
		 -H 'X-DTAS-Time:1464825757'\
		 https://$IP/web_service/sample_upload/unregister

